<?php
session_start();
	require_once('inc/constants.inc.php');
	require 'src/facebook.php';
	// App name Uttu
	// Create our Application instance (replace this with your appId and secret).
	$facebook = new Facebook(array(
	  'appId'  => APP_ID, //'334619919999877',
	  'secret' => APP_SECRET, //'ec96fb04e9c2ae9782718e1a4cfbc946',
	));

	// Get User ID
	$user = $facebook->getUser();
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

include 'google-openid/config/functions.php';
if (!empty($profile['first_name']) && !empty($profile['last_name']) && !empty($profile['id'])) 
{    
    $username = $profile['name'];
    $first_name = $profile['first_name'];
    $last_name =  $profile['last_name'];
    $email = $profile['email'];
	$gender = $profile['gender'];
    //$uid = $_GET['openid_ext1_value_uid'];
    $uid = null;
    $user = new User();
    $userdata = $user->checkUserGoogle('1', 'Facebook', $username, $email, $first_name, $last_name);
	
    
    if(!empty($userdata)) {
       // session_start();
        $_SESSION['id'] = $userdata['id'];
        $_SESSION['oauth_id'] = $uid;
        $_SESSION['login'] = "true";
		$_SESSION['logged_in'] = "true";
		$_SESSION['user_role'] = $userdata['user_role'];
        $_SESSION['username'] = $userdata['first_name']." ".$userdata['last_name'];
        $_SESSION['email'] = $userdata['email'];
        $_SESSION['oauth_provider'] = $userdata['oauth_provider'];
        
        header("Location: index.php");

    } else {
        // Something's missing, go back to square 1
        header('Location: error.php');
    }

}

								
?>